package xg.glycomaid.utils;

import java.io.*;
import java.util.*;

// Referenced classes of package org.eurocarbdb.application.glycanbuilder:
//            LogUtils

public class TextUtils
{

    public TextUtils()
    {
    }

    public static boolean isInteger(String text)
    {
        if(text == null || text.length() == 0)
            return false;
        try
        {
            Integer.parseInt(text);
            return true;
        }
        catch(Exception e)
        {
            return false;
        }
    }

    public static boolean isPositiveInteger(String text)
    {
        if(text == null || text.length() == 0)
            return false;
        try
        {
            return Integer.parseInt(text) >= 0;
        }
        catch(Exception e)
        {
            return false;
        }
    }

    public static String consume(InputStream is)
    {
        try
        {
            BufferedInputStream bis = new BufferedInputStream(is);
            StringBuilder str = new StringBuilder();
            int read;
            while((read = bis.read()) != -1) 
                str.append((char)read);
            return str.toString();
        }
        catch(Exception e)
        {
            LogUtils.report(e);
        }
        return "";
    }

    public static String invert(String str)
    {
        if(str == null || str.length() == 0)
            return str;
        StringBuilder ret = new StringBuilder();
        for(int i = str.length() - 1; i >= 0; i--)
            ret.append(str.charAt(i));

        return ret.toString();
    }

    public static String delete(String str, char c)
    {
        if(str == null || str.length() == 0)
            return str;
        StringBuilder ret = new StringBuilder();
        for(int i = 0; i < str.length(); i++)
            if(str.charAt(i) != c)
                ret.append(str.charAt(i));

        return ret.toString();
    }

    public static String squeeze(String str, char c)
    {
        if(str == null || str.length() == 0)
            return str;
        int start;
        for(start = 0; start < str.length() && str.charAt(start) == c; start++);
        if(start == str.length())
            return "";
        int end;
        for(end = str.length(); end > 0 && str.charAt(end - 1) == c; end--);
        return str.substring(start, end);
    }

    public static String squeezeAll(String str, char c)
    {
        if(str == null || str.length() == 0)
            return str;
        StringBuilder ret = new StringBuilder();
        char last_char = '\0';
        for(int i = 0; i < str.length(); i++)
        {
            if(i == 0 || str.charAt(i) != c || str.charAt(i) != last_char)
                ret.append(str.charAt(i));
            last_char = str.charAt(i);
        }

        return ret.toString();
    }
    
    public static String squeezeAllModified(String str, char c)
    {
        if(str == null || str.length() == 0)
            return str;
        StringBuilder ret = new StringBuilder();
        for(int i = 0; i < str.length(); i++)
            if(str.charAt(i) != c )
                ret.append(str.charAt(i));
        
        return ret.toString();
    }
    
    public static String trim(String str)
    {
        if(str == null || str.length() == 0)
        {
            return str;
        } else
        {
            str = squeeze(str, ' ');
            str = squeeze(str, '\t');
            str = squeeze(str, '\n');
            str = squeeze(str, '\r');
            return str;
        }
    }

    public static String trimModified(String str)
    {
        if(str == null || str.length() == 0)
        {
            return str;
        } else
        {
            str = squeezeAllModified(str, ' ');
            str = squeezeAllModified(str, '\t');
            str = squeezeAllModified(str, '\n');
            str = squeezeAllModified(str, '\r');
            return str;
        }
    }
    
    public static Vector tokenize(String str, String delims)
    {
        Vector out = new Vector();
        if(str == null || str.length() == 0)
            return out;
        StringBuilder token = new StringBuilder(str.length());
        for(int i = 0; i < str.length(); i++)
            if(find(delims, str.charAt(i)) != -1)
            {
                if(token.length() > 0)
                {
                    out.addElement(token.toString());
                    token = new StringBuilder(str.length());
                }
            } else
            {
                token.append(str.charAt(i));
            }

        if(token.length() > 0)
            out.addElement(token.toString());
        return out;
    }

    public static Vector tokenize(String str, String delims, char open_par, char closed_par)
    {
        Vector out = new Vector();
        if(str == null || str.length() == 0)
            return out;
        StringBuilder token = new StringBuilder(str.length());
        for(int i = 0; i < str.length(); i++)
        {
            if(str.charAt(i) == open_par)
            {
                token.append(str.charAt(i));
                for(i++; i < str.length() && str.charAt(i) != closed_par; i++)
                    token.append(str.charAt(i));

                if(i < str.length())
                    token.append(str.charAt(i));
                continue;
            }
            if(find(delims, str.charAt(i)) != -1)
            {
                if(token.length() > 0)
                {
                    out.addElement(token.toString());
                    token = new StringBuilder(str.length());
                }
            } else
            {
                token.append(str.charAt(i));
            }
        }

        if(token.length() > 0)
            out.addElement(token.toString());
        return out;
    }

    public static Vector splitLines(String str)
    {
        Vector v = new Vector();
        if(str == null || str.length() == 0)
            return v;
        BufferedReader br = new BufferedReader(new StringReader(str));
        String line;
        try
        {
            while((line = br.readLine()) != null) 
                v.addElement(line);
        }
        catch(IOException ex)
        {
            LogUtils.report(ex);
        }
        return v;
    }

    public static int find(String str, char c)
    {
        if(str == null || str.length() == 0)
            return -1;
        for(int i = 0; i < str.length(); i++)
            if(str.charAt(i) == c)
                return i;

        return -1;
    }

    public static int findEnclosed(String str)
        throws Exception
    {
        return findEnclosed(str, 0, '(', ')');
    }

    public static int findEnclosed(String str, int start, char open_par, char closed_par)
        throws Exception
    {
        if(str == null || str.length() == 0)
            return -1;
        if(str.charAt(start) != open_par)
            return -1;
        else
            return findClosedParenthesis(str, start + 1, open_par, closed_par);
    }

    public static int findClosedParenthesis(String str)
    {
        return findClosedParenthesis(str, 0, '(', ')');
    }

    public static int findClosedParenthesis(String str, int start_from)
    {
        return findClosedParenthesis(str, start_from, '(', ')');
    }

    public static int findClosedParenthesis(String str, int start_from, char open_par, char closed_par)
    {
        if(str == null || str.length() == 0)
            return -1;
        int nopen = 0;
        for(int i = start_from; i < str.length(); i++)
        {
            if(str.charAt(i) == closed_par)
            {
                if(nopen == 0)
                    return i;
                nopen--;
                continue;
            }
            if(str.charAt(i) == open_par)
                nopen++;
        }

        return -1;
    }

    public static int findEnclosedInvert(String str, int start, char open_par, char closed_par)
        throws Exception
    {
        if(str == null || str.length() == 0)
            return -1;
        if(str.charAt(start) != closed_par)
            return -1;
        else
            return findClosedParenthesisInvert(str, start - 1, open_par, closed_par);
    }

    public static int findClosedParenthesisInvert(String str, int start_from, char open_par, char closed_par)
    {
        if(str == null || str.length() == 0)
            return -1;
        int nopen = 0;
        for(int i = start_from; i >= 0; i--)
        {
            if(str.charAt(i) == open_par)
            {
                if(nopen == 0)
                    return i;
                nopen--;
                continue;
            }
            if(str.charAt(i) == closed_par)
                nopen++;
        }

        return -1;
    }

    public static String removeTrailingParentheses(String str)
    {
        if(str == null || str.length() == 0)
            return str;
        String ret = str;
        try
        {
            while(ret.startsWith("(") && ret.endsWith(")") && findClosedParenthesis(ret, 1) == ret.length() - 1) 
                ret = ret.substring(1, ret.length() - 1);
        }
        catch(Exception e)
        {
            LogUtils.report(e);
        }
        return ret;
    }

    public static int findFirstOf(String str, String chars)
    {
        if(str == null || str.length() == 0)
            return -1;
        for(int i = 0; i < str.length(); i++)
            if(find(chars, str.charAt(i)) != -1)
                return i;

        return -1;
    }

    public static int findFirstOfWithParentheses(String str, String chars)
        throws Exception
    {
        if(str == null || str.length() == 0)
            return -1;
        for(int i = 0; i < str.length(); i++)
        {
            if(str.charAt(i) == '(')
            {
                i = findClosedParenthesis(str, i + 1);
                if(i == -1)
                    throw new Exception((new StringBuilder()).append("Unmatched parenthesis in : ").append(str).toString());
            }
            if(find(chars, str.charAt(i)) != -1)
                return i;
        }

        return -1;
    }

    public static String toString(int v[], char delim)
    {
        if(v == null)
            return "";
        StringBuilder strbuf = new StringBuilder();
        for(int i = 0; i < v.length; i++)
        {
            if(i > 0)
                strbuf.append(delim);
            strbuf.append(v[i]);
        }

        return strbuf.toString();
    }

    public static String toString(Object v[], char delim)
    {
        if(v == null)
            return "";
        StringBuilder strbuf = new StringBuilder();
        for(int i = 0; i < v.length; i++)
        {
            if(i > 0)
                strbuf.append(delim);
            strbuf.append(v[i].toString());
        }

        return strbuf.toString();
    }

    public static String toString(Collection v, char delim)
    {
        if(v == null)
            return "";
        StringBuilder strbuf = new StringBuilder();
        Object o;
        for(Iterator i$ = v.iterator(); i$.hasNext(); strbuf.append(o.toString()))
        {
            o = i$.next();
            if(strbuf.length() > 0)
                strbuf.append(delim);
        }

        return strbuf.toString();
    }

    public static String toGreek(char c)
    {
        StringBuilder txt = new StringBuilder();
        if(Character.isLetter(c))
            txt.appendCodePoint((945 + c) - 97);
        else
            txt.append(c);
        return txt.toString();
    }
}

